package com.chtw.oasystem.service.impl;

import com.chtw.oasystem.bean.ChangeLogs;
import com.chtw.oasystem.bean.FullUserInfo;
import com.chtw.oasystem.bean.SocialSecurity;
import com.chtw.oasystem.bean.SocialSecurityExample;
import com.chtw.oasystem.common.changelogs.ChangeLogsUtils;
import com.chtw.oasystem.mapper.SocialSecurityMapper;
import com.chtw.oasystem.service.ChangeLogService;
import com.chtw.oasystem.service.SocialSecurityService;
import com.chtw.oasystem.utils.Session;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author chtw
 * @date 2020-11-20 22:47
 */
@Service
public class SocialSecurityServiceImpl implements SocialSecurityService {

    @Autowired
    private SocialSecurityMapper socialSecurityMapper;

    @Autowired
    private ChangeLogService changeLogService;

    @Override
    public PageInfo<SocialSecurity> list(String search, int page, int size) {
        try {
            PageHelper.startPage(page, size);
            SocialSecurityExample example = new SocialSecurityExample();
            if (search != null) {
                SocialSecurityExample.Criteria re = example.createCriteria();
                re.andUserNameLike("%" + search + "%");
            }
            List<SocialSecurity> res = socialSecurityMapper.selectByExample(example);
            return new PageInfo<>(res);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public SocialSecurity info(int id) {
        return socialSecurityMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean create(HttpSession session, SocialSecurity socialSecurity) {
        FullUserInfo currentUser = Session.getCurrentUser(session);
        if (currentUser == null) {
            throw new RuntimeException("用户未登录");
        } else if (currentUser.getRoleId() < 1) {
            int created = socialSecurityMapper.insert(socialSecurity);
            if (created > 0) {
                ChangeLogs changeLog = ChangeLogsUtils.generateSocialSecurityChangeLog(currentUser.getUserName(), socialSecurity.getUserName(), "增加");
                changeLogService.saveChangeLog(changeLog);
                return true;
            }
            return false;
        } else {
            throw new RuntimeException("没有权限！");
        }
    }

    @Override
    public boolean edit(HttpSession session, SocialSecurity socialSecurity) {
        FullUserInfo currentUser = Session.getCurrentUser(session);
        if (currentUser == null) {
            throw new RuntimeException("用户未登录");
        } else if (currentUser.getRoleId() < 1) {
            int isEdit = socialSecurityMapper.updateByPrimaryKeySelective(socialSecurity);
            if (isEdit > 0) {
                ChangeLogs changeLog = ChangeLogsUtils.generateSocialSecurityChangeLog(currentUser.getUserName(), socialSecurity.getUserName(), "增加");
                changeLogService.saveChangeLog(changeLog);
                return true;
            }
            return false;
        } else {
            throw new RuntimeException("没有权限！");
        }
    }

    @Override
    public boolean delete(HttpSession session, int id) {
        FullUserInfo currentUser = Session.getCurrentUser(session);
        if (currentUser == null) {
            throw new RuntimeException("用户未登录");
        } else if (currentUser.getRoleId() < 1) {
            SocialSecurity socialSecurity = socialSecurityMapper.selectByPrimaryKey(id);
            int isEdit = socialSecurityMapper.deleteByPrimaryKey(id);
            if (isEdit > 0) {
                ChangeLogs changeLog = ChangeLogsUtils.generateSocialSecurityChangeLog(currentUser.getUserName(), socialSecurity.getUserName(), "删除");
                changeLogService.saveChangeLog(changeLog);
                return true;
            }
            return false;
        } else {
            throw new RuntimeException("没有权限！");
        }
    }
}
